import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {PerformanceProvider} from "../../../providers/perfomance/performance/performance";
import {TimeUtil} from "../../../model/util/TimeUtil";
import {HttpErrorResponse} from "@angular/common/http";
import {PersonalGroupPerformancePage} from "../personal-group-performance/personal-performance";
import {PerformanceType} from "../../../model/enum/performance";
import {AchieveDetail} from "../../../model/pages/performance/group-achieve";
import {LoadingService} from "../../../services/common/loading.service";

/**
 * Generated class for the PersonalPerformanceListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-personal-performance-list',
    templateUrl: 'personal-performance-list.html',
    providers: [PerformanceProvider, LoadingService]
})
export class PersonalPerformanceListPage {
    /**
     * 筛选时间
     * @type {string}
     */
    filterDate = '';
    /**
     * 组id
     * @type {string}
     */
    groupId = '';
    /**
     * 组业绩列表数据
     * @type {any[]}
     */
    personalPerformanceList: Array<AchieveDetail> = [];
    /**
     * 是否在记载数据
     * @type {boolean}
     */
    isLoaded: boolean = false;

    /**
     * 获取数据订阅
     */
    getDataSubscription: any;

    @ViewChild(Content) content: Content;

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                private performanceProvider: PerformanceProvider,
                private toastController: ToastController,
                private loadingService: LoadingService) {
    }

    ionViewDidLoad() {
        // console.log('ionViewDidLoad PersonalPerformanceListPage');
        // 默认筛选时间为当前月份
        this.filterDate = TimeUtil.getLocalTime(Date.now());
        // 组id
        this.groupId = this.navParams.get("id");
        // 查询月份
        let month = this.navParams.get("month");
        if (month) {
            this.filterDate = month;
        }

        // 开始查询
        this.filter();
    }

    goBack() {
        this.navCtrl.pop();
    }

    /**
     * 查询所选时间的业绩
     */
    filter() {
        // 显示loading动画
        this.loadingService.showLoading();
        // 请求数据
        this.getDataSubscription = this.performanceProvider.getPersonalAchieveList(this.groupId, this.filterDate).subscribe(data => {


            this.personalPerformanceList = data;
            this.isLoaded = true;

            // 关闭loading动画
            this.loadingService.dismissLoading();
        }, (error: HttpErrorResponse) => {
            this.isLoaded = true;
            this.loadingService.dismissLoading();
            this.personalPerformanceList = []
        });

    }

    ionViewWillLeave() {
        // 取消订阅数据
        if (this.getDataSubscription) {
            this.getDataSubscription.unsubscribe();
        }
    }

    /**
     * 跳转至个人业绩
     * @param {AchieveDetail} data
     */
    goPersonalPerformancePage(data: AchieveDetail) {
        this.navCtrl.push("PersonalGroupPerformancePage", {
            type: PerformanceType.Person,
            data: data,
            month: this.filterDate
        })
    }

    /**
     * 移至顶部
     */
    goUp() {
        this.content.scrollToTop();
    }


}
